import 'package:xws/helpers/as_t.dart';

/// 物品AI识别信息
class GoodsInfo {
  GoodsInfo({
    required this.logId,
    required this.errorMsg,
    required this.errorCode,
    required this.resultNum,
    required this.result,
  });

  factory GoodsInfo.fromJson(Map<String, dynamic> jsonRes) {
    final List<ResultInfo> result = <ResultInfo>[];

    if (jsonRes['result'] != null)
      for (final dynamic item in jsonRes['result']) {
        if (item != null) result.add(ResultInfo.fromJson(asT<Map<String, dynamic>>(item)));
      }

    return GoodsInfo(
      logId: asT<int>(jsonRes['log_id']),
      errorMsg: asT<String>(jsonRes['error_msg']),
      errorCode: asT<int>(jsonRes['error_code']),
      resultNum: asT<int>(jsonRes['result_num']),
      result: result,
    );
  }

  late int logId;
  late String errorMsg;
  late int errorCode;
  late int resultNum;
  late List<ResultInfo> result;
}

class ResultInfo {
  ResultInfo({
    required this.score,
    required this.root,
    required this.keyword,
  });

  factory ResultInfo.fromJson(Map<String, dynamic> jsonRes) => ResultInfo(
        score: asT<double>(jsonRes['score']),
        root: asT<String>(jsonRes['root']),
        keyword: asT<String>(jsonRes['keyword']),
      );

  late double score;
  late String root;
  late String keyword;
}
